#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Premiere.Window;
using Premiere.Framework;
using System.Collections;
using Premiere.DTO;

#endregion

namespace PremiereMX
{
    public partial class SearchPartForm : Form
    {
        IList fs;
        PartDTO m_part;
        public delegate void Execute(PartDTO part, int row);
        Execute m_t;
        Keys keycode = Keys.A;
        int m_row;

        public SearchPartForm(Execute t, int row)
        {
            InitializeComponent(); 
            fs = ServiceRegistry.GetInstance().GetPartService().GetFieldInfos();
            m_t = t;
            m_row = row;
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            ArrayList result = new ArrayList();
            dataGridView1.Rows.Clear();
            foreach (FieldInfo fi in fs)
            {
                fi.Value = textBox1.Text.Trim();
                IList found = ServiceRegistry.GetInstance().GetPartService().FindByField(fi);
                result.AddRange(found);
            }
            foreach (PartDTO p in result)
            {
                dataGridView1.Rows[dataGridView1.Rows.Add(p.Code, p.Name)].Tag = p;

            }
                
        }
        public PartDTO PartDTO
        {
            get { return m_part; }
        }
        private void buttonSelect_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows[0] != null)
            {
                m_part = (PartDTO)dataGridView1.SelectedRows[0].Tag;
                this.Close();
                m_t(m_part, m_row);
            }
        }

        private void textBox1_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_KeyUp(object objSender, KeyEventArgs objArgs)
        {
            keycode = objArgs.KeyCode;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (keycode == Keys.Enter)
            {
                buttonFind_Click(null, null);
            }
        }
    }
}